/**
 * 
 */
package cn.jy.erp.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.jy.erp.domain.AccountsPayable;
import cn.jy.erp.domain.FnancialPlans;
import cn.jy.erp.domain.IncomePay;
import cn.jy.erp.domain.IncomeStacking;
import cn.jy.erp.domain.SZResult;
import cn.jy.erp.domain.Sale;
import cn.jy.erp.domain.SaleIncome;
import cn.jy.erp.domain.SalePurchase;
import cn.jy.erp.domain.SjjeTotal;
import cn.jy.erp.domain.SzName;
import cn.jy.erp.domain.WeekConvert;
import cn.jy.erp.mapper.FnancialPlansMapper;
import cn.jy.erp.mapper.SalePurchaseMapper;
import cn.jy.erp.service.FnancialPlansService;
import cn.jy.erp.util.DateUtil;

/**
 * @author 刘中斐
 *@date 2018年7月26日 上午10:19:58 
 */
@Service
public class FnancialPlansServiceimpl implements FnancialPlansService {

	@Autowired
	FnancialPlansMapper fnancialPlansMapper;
	@Autowired
	SalePurchaseMapper salePurchaseMapper;



	@Override
	public List<Object> getjy() throws Exception {
		updateWeek();
		updateWeek1();
		updateSjWeek();
		updateSjTotal();
		updateSaleWeek();
		updateIncome();
		updatePay();
		updatePurchase();
		updateSale();
		updateSjjeTotal();

		List<SZResult> list=fnancialPlansMapper.getCalculation();
		List<SalePurchase> cg =salePurchaseMapper.getBytotal();
		List<Object> result=new ArrayList<Object>();

		List<SaleIncome> incomes=new ArrayList<SaleIncome>();
		double tmp=0;
		double tmp1=0;
		double tmp3=0;
		double tmp4=0;
		double next=0;
		double next1=0;
		double sr=0;
		double sr1=0;
		Set<String> set=new HashSet<String>();
		Set<String>szSet=new HashSet<String>();
		Set<String>saSet=new HashSet<String>();			
		for (SZResult sz : list) {
			set.add(sz.getWhereWeek());
			szSet.add(sz.getWhereWeek());
		}
		for (SalePurchase sa : cg) {
			set.add(sa.getWhereWeek());
			saSet.add(sa.getWhereWeek());
		}

		List<String>  Alllist = new ArrayList<String>(set);
		if (Alllist.size()>0) {
			Alllist.remove(null);
			Collections.sort(Alllist);  
		}


		for (int i = 0; i < Alllist.size(); i++) {
			for (SZResult sz : list) {
				if (sz.getWhereWeek()!=null&&Alllist.get(i)!=null) {
					if (sz.getTotal()!=null) {
						if (sz.getWhereWeek().equals(Alllist.get(i))) {
							if (sz.getSZLX()==1) {
								tmp=Double.valueOf(sz.getTotal());
								if (sz.getSJTOTAL()!=null) {
									tmp3=Double.valueOf(sz.getSJTOTAL());
								}

							}
							if (sz.getSZLX()==2) {
								tmp1=Double.valueOf(sz.getTotal());
								if (sz.getSJTOTAL()!=null) {
									tmp4=Double.valueOf(sz.getSJTOTAL());
								}

							}
						}
					}
				}
			}	
			for (SalePurchase sale : cg) {
				if (sale.getWhereWeek()!=null&&Alllist.get(i)!=null) {
					if (sale.getTotal()!=null) {
						if (sale.getWhereWeek().equals(Alllist.get(i))) {
							if (sale.getRWLX()!=null) {


								if (sale.getRWLX().equals("1")) {
									tmp1+=sale.getTotal();
									if (String.valueOf(sale.getSjtotal())==null) {
										sale.setSjtotal(0);
									}
									tmp4+=sale.getSjtotal();
								}
								if (sale.getRWLX().equals("2")) {
									tmp+=sale.getTotal();
									if (String.valueOf(sale.getSjtotal())==null) {
										sale.setSjtotal(0);
									}
									tmp3+=sale.getSjtotal();
								}
							}
						}
					}

				}

			}

			if (i==0) {
				next=tmp-tmp1;
				next1=tmp3-tmp4;
			}else {
				sr=next+tmp;
				next=sr-tmp1;
				sr1=next1+tmp3;
				next1=sr1-tmp4;
			}
			if (tmp3==0&&tmp4==0) {
				next1=0;
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("jhjy",next);
			map.put("sjjy",next1);
			map.put("whereWeek", Alllist.get(i));
			int year=Integer.valueOf(Alllist.get(i).toString().substring(0,4));

			int week=Integer.valueOf(Alllist.get(i).toString().substring(5,7));
			map.put("weekDate",DateUtil.getWeekDays(year,week,0));
			result.add(map);			
			tmp=0;
			tmp1=0;
			tmp3=0;
			tmp4=0;
		}
		return result;
	}	

	@Override
	public List<Object> getdiff() throws Exception {
		//和采购表同步使其周数相同
		List<SZResult> list=fnancialPlansMapper.getCalculation();
		List<SalePurchase> cg =salePurchaseMapper.getPurchase();
		Set<String> set=new HashSet<String>();
		Set<String>szSet=new HashSet<String>();
		Set<String>saSet=new HashSet<String>();
		for (SZResult sz : list) {
			if (sz.getWhereWeek()!=null) {
				set.add(sz.getWhereWeek());
				szSet.add(sz.getWhereWeek());
			}

		}
		for (SalePurchase sa : cg) {
			if (sa.getWhereWeek()!=null) {
				set.add(sa.getWhereWeek());
				saSet.add(sa.getWhereWeek());
			}

		}
		List<String>  Alllist = new ArrayList<String>(set);
		List<String>  szlist = new ArrayList<String>(szSet);
		List<String> newSzList=DateUtil.removeList(Alllist, szlist);
		for (String string : newSzList) {
			SZResult sz=new SZResult();
			sz.setWhereWeek(string);
			sz.setTotal("0");
			sz.setSJTOTAL("0");
			list.add(sz);
		}
		double tmp=0;
		double tmp1=0;
		double tmp3=0;
		double tmp4=0;
		List<Object> result=new ArrayList<Object>(); 
		//Map<String, Object> map=null;
		Collections.sort(Alllist);
		for (String str : Alllist) {	

			for (SZResult sz : list) {
				//System.out.println(sz.getSJTOTAL());
				if (str!=null&&sz.getWhereWeek()!=null) {
					if (sz.getWhereWeek().equals(str)) {
						if (sz.getTotal()!=null) {				
							if (sz.getSZLX()==1) {
								tmp=Double.valueOf(sz.getTotal());
								if (sz.getSJTOTAL()!=null&&!"0".equals(sz.getSJTOTAL())) {
									tmp3=Double.valueOf(sz.getSJTOTAL());
								}

							}
							if (sz.getSZLX()==2) {
								tmp1=Double.valueOf(sz.getTotal());	
								if (sz.getSJTOTAL()!=null&&!"".equals(sz.getSJTOTAL())) {
									tmp4=Double.valueOf(sz.getSJTOTAL());
								}
							}				
						}

					}
				}

			}
			//			if (!"0.0".equals(tmp)) {
			//				System.out.println(tmp);
			//			}

			Map<String, Object> map=new HashMap<String, Object>();
			map.put("whereWeek", str);
			map.put("sr", tmp);
			map.put("zc", tmp1);
			map.put("sjsr",tmp3);
			map.put("sjzc",tmp4);
			result.add(map);
			tmp=0;
			tmp1=0;
			tmp3=0;
			tmp4=0;
		}

		/*		for (int i = 0; i <list.size(); i++) {
			if (list.get(i).getTotal()!=null) {
				if (list.get(i).getWhereWeek()!=null) {
					if (list.get(i).getWhereWeek().equals(list.get(i-1).getWhereWeek())) {
						if (list.get(i).getSZLX()==1) {
							tmp=Double.valueOf(list.get(i).getTotal());
						}else if (list.get(i-1).getSZLX()==1) {
							if (list.get(i-1).getTotal()!=null) {
								tmp=Double.valueOf(list.get(i-1).getTotal());
							}							
						}
						if (list.get(i).getSZLX()==2) {
							tmp1=Double.valueOf(list.get(i).getTotal());
						}else if (list.get(i+1).getSZLX()==2) {
							tmp1=Double.valueOf(list.get(i+1).getTotal());
						}
					}else{
						if (i<list.size()-1) {
							if (!list.get(i).getWhereWeek().equals(list.get(i+1).getWhereWeek())&&!list.get(i).getWhereWeek().equals(list.get(i-1).getWhereWeek())) {
								if (list.get(i).getSZLX()==1) {
									tmp=Double.valueOf(list.get(i).getTotal());
									tmp1=0;
								}else {
									tmp1=Double.valueOf(list.get(i).getTotal());
									tmp=0;
								}			
							}else {
								continue;
							}
						}


					}
				}


				if (i==0) {
					if (list.get(i).getSZLX()==1) {
						tmp=Double.valueOf(list.get(i).getTotal());
						tmp1=0;
					}else {
						tmp1=Double.valueOf(list.get(i).getTotal());
						tmp=0;
					}			
					next=tmp-tmp1;
				}else {
					sr=next+tmp;
					next=sr-tmp1;
				}			
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("whereWeek", list.get(i).getWhereWeek());
				map.put("sr", tmp);
				map.put("zc", tmp1);
				map.put("jy", next);
				result.add(map);
				tmp=0;
				tmp1=0;

			}
		}*/
		return result;
	}

	/**
	 * 获取收支统计详细信息
	 */
	@Override
	public List<FnancialPlans> getBySZInformation(String type,String whereWeek) throws ParseException {
		List<FnancialPlans> bySZInformation=null;
		if (type.equals("1")||type.equals("2")) {
			bySZInformation = fnancialPlansMapper.getBySZInformation(type, whereWeek);
		}else {
			if (type.equals("3")) {
				type="1";
			}else {
				type="2";
			}
			bySZInformation = fnancialPlansMapper.getBySJInformation(type, whereWeek);
		}


		return bySZInformation;
	}
	/**
	 * 修改收支周
	 * @throws ParseException
	 */
	private void updateWeek() throws ParseException{
		List<FnancialPlans> list=fnancialPlansMapper.getPlans();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		SimpleDateFormat df = new SimpleDateFormat("yyyy"); 
		for (int i = 0; i < list.size(); i++) {	
			SZResult sz=new SZResult();
			if (list.get(i).getIecode()!=null) {
				if (list.get(i).getJhyf() !=null) {
					if (sdf.format(list.get(i).getJhyf()).substring(0,4).equals(df.format(new Date()))) {

						sz.setWhereWeek(DateUtil.getWeek2(list.get(i).getJhyf()));

						/*if (sdf.format(list.get(i).getJhyf()).substring(5, 10).equals("12-31")) {
							sz.setWhereWeek(df.format(new Date())+":"+"52");
						}*/

						sz.setIECODE(list.get(i).getIecode().toString());
						fnancialPlansMapper.updateWeek(sz);
					}else {

						sz.setWhereWeek(DateUtil.getWeek2(list.get(i).getJhyf()));

						sz.setIECODE(list.get(i).getIecode().toString());
						fnancialPlansMapper.updateWeek(sz);
					}
				}
			}
		}
	}



	/**
	 * 修改收入总额
	 */
	private void updateIncome() throws Exception {
		List<FnancialPlans> list=fnancialPlansMapper.getPlans();
		List<SZResult> result=new ArrayList<SZResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		HashSet<String> aEs=new HashSet<String>();
		for (int i = 0; i < list.size(); i++) {		
			if (list.get(i).getSzlx()!=null&&!"".equals(list.get(i).getSzlx())) {
				if (list.get(i).getSzlx()==1) {
					SZResult sz= new SZResult();	
					sz.setWhereWeek(list.get(i).getWhereweek());
					sz.setIENAME(list.get(i).getIename());
					sz.setJHJE(list.get(i).getJhje());		
					sz.setJHYF(sdf.format(list.get(i).getJhyf()));
					if (list.get(i).getSjje()!=null) {
						sz.setSjje(list.get(i).getSjje());
					}else {
						sz.setSjje(0.0);
					}					
					sz.setSZLX(1);
					result.add(sz);
				}	
			}
		}
		for (SZResult sZResult : result) {
			System.out.println(sZResult);
			aEs.add(sZResult.getWhereWeek());
		}
		double tmp=0;
		double tmp1=0;
		double tmp3=0;
		List<String>  aEsList = new ArrayList<String>(aEs);

		for (String string : aEsList) {	
			System.out.println(string);
			for (SZResult sz : result) {
				if (sz.getWhereWeek()!=null&&string!=null) {
					if (sz.getWhereWeek().equals(string)) {
						System.out.println(sz.getJHJE());
						//tmp3+=sz.getJHJE();
						tmp+=sz.getJHJE();
						tmp1+=sz.getSjje();
					}
				}

			}

			IncomePay incomePay=new IncomePay();
			incomePay.setTotal(tmp);
			incomePay.setWhereWeek(string);
			incomePay.setSjtotal(tmp1);
			incomePay.setSzlx(1);
			//System.out.println(tmp3);
			fnancialPlansMapper.updatePay(incomePay);
			System.out.println("更新收入成功");
			tmp=0;
			tmp1=0;

		}
	}
	/**
	 * 修改支出总额
	 */

	private void updatePay() throws Exception {
		List<FnancialPlans> list=fnancialPlansMapper.getPlans();
		List<SZResult> result=new ArrayList<SZResult>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		HashSet<String> aEs=new HashSet<String>();
		for (int i = 0; i < list.size(); i++) {		
			if (list.get(i).getSzlx()!=null&&!"".equals(list.get(i).getSzlx())) {
				if (2==list.get(i).getSzlx()) {
					SZResult sz= new SZResult();
					sz.setJHYF(sdf.format(list.get(i).getJhyf()));
					sz.setWhereWeek(list.get(i).getWhereweek());
					if (list.get(i).getSjje()!=null) {
						sz.setSjje(list.get(i).getSjje());
					}else {
						sz.setSjje(0.0);
					}				
					sz.setIENAME(list.get(i).getIename());
					sz.setJHJE(list.get(i).getJhje());	
					sz.setSZLX(2);
					result.add(sz);
				}
			}
		}
		for (SZResult sZResult : result) {
			aEs.add(String.valueOf(sZResult.getWhereWeek()));
		}
		double tmp=0;
		double tmp1=0;
		double tmp3=0;
		List<String>  aEsList = new ArrayList<String>(aEs);
		for (String string : aEsList) {	
			for (SZResult sz : result) {
				if ( String.valueOf(sz.getWhereWeek()).equals(string)) {
					tmp3+=sz.getJHJE();
					tmp+=sz.getJHJE();				
					//					tmp1+=sz.getSjje();
				}
			}	

			IncomePay incomePay=new IncomePay();
			incomePay.setTotal(tmp);
			incomePay.setWhereWeek(string);
			//			incomePay.setSjtotal(tmp1);
			incomePay.setSzlx(2);

			fnancialPlansMapper.updatePay(incomePay);
			System.out.println("更新支出成功");
			tmp=0;
			tmp1=0;
		}

	}

	/**
	 * 修改销售采购周
	 * @throws ParseException
	 */
	private void updateSaleWeek() throws ParseException{
		List<SalePurchase> list=salePurchaseMapper.getSaleDate();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
		SimpleDateFormat df = new SimpleDateFormat("yyyy"); 
		for (int i = 0; i < list.size(); i++) {	
			SalePurchase sz=new SalePurchase();
			if (list.get(i).getSpjhsfsj()!=null) {				
				if ("1".equals(list.get(i).getRWLX())) {
					sz.setWhereWeek(DateUtil.getWeek2(list.get(i).getSpjhsfsj()));
				}					
				//					if (sdf.format(list.get(i).getSpjhsfsj()).substring(5, 10).equals("12-31")) {
				//						sz.setWhereWeek(df.format(new Date())+":"+"52");
				//					}
				sz.setTid(list.get(i).getTid());
				salePurchaseMapper.updateByWeek(sz);
				System.out.println("周修改成功");

				sz.setWhereWeek(DateUtil.getWeek2(list.get(i).getSpjhsfsj()));					
				sz.setTid(list.get(i).getTid());
				salePurchaseMapper.updateByWeek(sz);

			}

		}
	}
	/**
	 * 修改采购总额
	 * @throws Exception
	 */
	private void updatePurchase() throws Exception {
		updateRWLX();
		List<SalePurchase> list =salePurchaseMapper.getPurchase();
		HashSet<String> aEs=new HashSet<String>();
		for (SalePurchase sa : list) {
			aEs.add(sa.getWhereWeek());
		}
		double tmp=0;
		double tmp1=0;
		double tmp2=0;
		List<String>  aEsList = new ArrayList<String>(aEs);
		for (String string : aEsList) {			
			for (SalePurchase sa : list) {
				if (sa.getWhereWeek()!=null) {
					if (sa.getWhereWeek().equals(string)) {
						tmp+=sa.getSpjhzj();		
						tmp2+=sa.getSpjhzj();
						if (sa.getSpsjzj()!=null&&sa.getSFKRQ()!=null) {
							tmp1+=sa.getSpsjzj();
						}	
					}
				}	
			}
			SalePurchase sale=new SalePurchase();
			sale.setSjtotal(tmp1);
			sale.setTotal(tmp);
			sale.setWhereWeek(string);
			sale.setRWLX("1");
			salePurchaseMapper.updateByTotal(sale);
			System.out.println("更新采购金额成功");
			tmp=0;
			tmp1=0;
		}

		System.out.println(tmp2);
	}

	/**
	 * 修改销售总额
	 * @throws Exception
	 */
	private void updateSale() throws Exception {
		updateRWLX();
		List<SalePurchase> list =salePurchaseMapper.getSale();
		HashSet<String> aEs=new HashSet<String>();
		for (SalePurchase sa : list) {
			aEs.add(sa.getWhereWeek());
		}
		double tmp=0;
		double tmp1=0;
		double tmp2=0;
		List<String>  aEsList = new ArrayList<String>(aEs);
		for (String string : aEsList) {			
			for (SalePurchase sa : list) {
				if (sa.getWhereWeek()!=null) {
					if (sa.getWhereWeek().equals(string)) {
						tmp+=sa.getSpjhzj();
						tmp2+=sa.getSpjhzj();
						if (sa.getSpsjzj()!=null) {
							tmp1+=sa.getSpsjzj();
						}	
					}
				}	
			}
			SalePurchase sale=new SalePurchase();
			sale.setSjtotal(tmp1);
			sale.setTotal(tmp);
			sale.setWhereWeek(string);
			sale.setRWLX("2");
			salePurchaseMapper.updateByTotal(sale);
			System.out.println("更新销售总额成功");
			tmp=0;
			tmp1=0;
		}
		System.out.println(tmp2);

	}


	@Override
	public List<Sale> getByTotal() throws Exception{
		List<SalePurchase> list =salePurchaseMapper.getBytotal();
		List<SZResult> sr=fnancialPlansMapper.getCalculation();
		List<Sale> sales= new ArrayList<Sale>();
		Set<String> set=new HashSet<String>();
		Set<String>szSet=new HashSet<String>();
		Set<String>saSet=new HashSet<String>();
		for (SZResult sz : sr) {
			if (sz.getWhereWeek()!=null) {
				set.add(sz.getWhereWeek());
				szSet.add(sz.getWhereWeek());
			}

		}
		for (SalePurchase sa : list) {
			if (sa.getWhereWeek()!=null) {
				set.add(sa.getWhereWeek());
				saSet.add(sa.getWhereWeek());
			}

		}
		List<String>  allList = new ArrayList<String>(set);
		List<String>  salist = new ArrayList<String>(saSet);
		List<String> newSaList=DateUtil.removeList(allList, salist);
		for (String string : newSaList) {
			SalePurchase salePurchase=new SalePurchase();
			salePurchase.setSjtotal(0);
			salePurchase.setTotal(0.0);
			salePurchase.setWhereWeek(string);
			salePurchase.setRWLX("1");
			list.add(salePurchase);
		}
		for (SalePurchase sa : list) {
			Sale sale=new Sale();
			if (sa.getWhereWeek()!=null) {
				if ("1".equals(sa.getRWLX())) {
					sale.setWhereWeek(sa.getWhereWeek());
					sale.setSaleJhTotal(sa.getTotal());
					sale.setSaleSjTotal(sa.getSjtotal());
				}else {
					sale.setWhereWeek(sa.getWhereWeek());
					sale.setTotal(sa.getTotal());
					sale.setSjTotal(sa.getSjtotal());
				}
				sales.add(sale);
			}
		}
		return sales;
	}

	@Override
	public Map<String, Object> getByDetailed(String whereWeek,String rwlx) {
		Map<String, Object> map=new HashMap<String, Object>();
		if ("1".equals(rwlx)||"2".equals(rwlx)) {
			List<SalePurchase> byDetailed = salePurchaseMapper.getByDetailed(whereWeek,rwlx);
			SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
			int time=0;
			for (SalePurchase salePurchase : byDetailed) {
				if (salePurchase.getPlanarrivedtime()!=null&&salePurchase.getSpjhsfsj()!=null) {
					try {
						long a=salePurchase.getSpjhsfsj().getTime()-sd.parse(salePurchase.getPlanarrivedtime()).getTime();
						time=(int) (a/1000/60/60/24);
						salePurchase.setDays(time);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			map.put("jh", byDetailed);
		}		
		if ("3".equals(rwlx)) {
			map.put("sj", salePurchaseMapper.getBySaleDetailed(whereWeek, "1"));		
		}
		if ("4".equals(rwlx)) {
			List<SalePurchase> byDetailed = salePurchaseMapper.getBySaleDetailed(whereWeek, "2");
			SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
			int time=0;
			for (SalePurchase salePurchase : byDetailed) {
				if (salePurchase.getSFKRQ()!=null&&salePurchase.getSFHRQ()!=null) {
					try {
						long a=sd.parse(salePurchase.getSFKRQ()).getTime()-sd.parse(salePurchase.getSFHRQ()).getTime();
						time=(int) (a/1000/60/60/24);
						salePurchase.setDays(time);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			map.put("sj",byDetailed );

		}	

		return map;
	}
	private void updateRWLX(){
		List<String> list=salePurchaseMapper.getByRwlx("1");
		for (String string : list) {
			SalePurchase sa=new SalePurchase();
			sa.setRWLX("1");
			sa.setTid(string);
			salePurchaseMapper.UpdateRwlx(sa);
		}
		List<String> list2 =salePurchaseMapper.getByRwlx("2");
		for (String string : list2) {
			SalePurchase sa=new SalePurchase();
			sa.setRWLX("2");
			sa.setTid(string);
			salePurchaseMapper.UpdateRwlx(sa);
		}
	}

	/* (non-Javadoc)
	 * @see cn.jy.erp.service.FnancialPlansService#getSaleIncome()
	 */
	@Override
	public List<SaleIncome> getSaleIncome() {
		List<SZResult> list=fnancialPlansMapper.getCalculation();
		List<SalePurchase> cg =salePurchaseMapper.getBytotal();
		List<SaleIncome> saleIncomes=new ArrayList<SaleIncome>();
		for (SZResult sz : list) {
			SaleIncome sa=new SaleIncome();
			sa.setSaleTotal(Double.valueOf(sz.getTotal()));
			sa.setSaleSjTotal(Double.valueOf(sz.getSJTOTAL()));
			sa.setWhereWeek(sz.getWhereWeek());
			sa.setSZType(sz.getSZLX());
			saleIncomes.add(sa);
		}
		for (SalePurchase salePurchase : cg) {
			SaleIncome sa=new SaleIncome();
			sa.setSaleTotal(salePurchase.getTotal());
			sa.setSaleSjTotal(salePurchase.getSjtotal());
			sa.setWhereWeek(salePurchase.getWhereWeek());
			saleIncomes.add(sa);
		}

		return saleIncomes;
	}

	@Override
	public Map<String, Object> getSZDtail() throws Exception{
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String startTime= sd.format(new Date())+"-01";
		String year=startTime.substring(0,4);
		String month=startTime.substring(5,7);
		String endTime = DateUtil.getLastDayOfMonth1(Integer.valueOf(year),Integer.valueOf(month));
		Map<String, Object> map=new HashMap<String, Object>();
		double CGmonthTotal=0;
		double XSmonthTotal=0;
		double ZCmonthTotal=0;
		double SRmonthTotal=0;
		double XSActualTotal=0;
		double CGActualTotal=0;
		double CGTotal=0;
		double XSTotal=0;
		double ZCTotal=0;
		double SRTotal=0;

		for (int i = 0; i <12; i++) {
			List<SalePurchase> mouthCGDetail = fnancialPlansMapper.getMouthCGDetail(DateUtil.getThatDate(startTime, i,2), DateUtil.getThatDate(endTime, i,2));
			for (SalePurchase salePurchase : mouthCGDetail) {
				if (salePurchase.getSpjhzj()!=null) {
					CGmonthTotal+=salePurchase.getSpjhzj();
				}
				if (salePurchase.getSpsjzj()!=null) {
					CGActualTotal+=salePurchase.getSpsjzj();
				}
			}

			map.put("CGMonth"+i,mouthCGDetail);
			map.put("CGMonthTotal"+i, CGmonthTotal);
			map.put("CGActualTotal"+i, CGActualTotal);
			CGmonthTotal=0;
			int time=0;
			List<SalePurchase> mouthXSDetail = fnancialPlansMapper.getMouthXSDetail(DateUtil.getThatDate(startTime, i,2), DateUtil.getThatDate(endTime, i,2));
			for (SalePurchase salePurchase : mouthXSDetail) {
				if (salePurchase.getSpjhzj()!=null) {
					XSmonthTotal+=salePurchase.getSpjhzj();
				}
				if (salePurchase.getSpsjzj()!=null) {
					XSActualTotal+=salePurchase.getSpsjzj();
				}


				if (salePurchase.getSpjhsfsj()!=null&&salePurchase.getPlanarrivedtime()!=null) {					
					long a=salePurchase.getSpjhsfsj().getTime()-sdf.parse(salePurchase.getPlanarrivedtime()).getTime();
					time=(int) (a/1000/60/60/24);
					salePurchase.setDays(time);

				}


			}
			map.put("XSMonth"+i, mouthXSDetail);
			map.put("XSMonthTotal"+i, XSmonthTotal);
			map.put("XSActualTotal"+i, XSActualTotal);
			XSmonthTotal=0;
			XSActualTotal=0;
			List<SZResult> mouthZCDetail = fnancialPlansMapper.getMouthZCDetail(DateUtil.getThatDate(startTime, i,2), DateUtil.getThatDate(endTime, i,2));
			for (SZResult szResult : mouthZCDetail) {
				if (szResult.getJHJE()!=null) {
					ZCmonthTotal+=szResult.getJHJE();
				}

			}
			/*SZResult sz=new SZResult();
			sz.setTotal(String.valueOf(ZCmonthTotal));
			mouthZCDetail.add(sz);*/
			map.put("ZCMonth"+i, mouthZCDetail);
			map.put("ZCMonthTotal"+i, ZCmonthTotal);
			ZCmonthTotal=0;
			List<SZResult> mouthSRDetail = fnancialPlansMapper.getMouthSRDetail(DateUtil.getThatDate(startTime, i,2), DateUtil.getThatDate(endTime, i,2));

			for (SZResult szResult : mouthSRDetail) {
				if (szResult.getJHJE()!=null) {
					SRmonthTotal+=szResult.getJHJE();
				}

			}
			/*SZResult szr=new SZResult();
			szr.setTotal(String.valueOf(SRmonthTotal));
			mouthSRDetail.add(szr);*/
			map.put("SRMonth"+i, mouthSRDetail);
			map.put("SRMonthTotal"+i, SRmonthTotal);
			SRmonthTotal=0;
		}
		List<SalePurchase> YearCGDetail = fnancialPlansMapper.getMouthCGDetail(DateUtil.getThatDate(startTime,0,1), DateUtil.getThatDate(endTime, 1,1));
		for (SalePurchase salePurchase : YearCGDetail) {
			CGTotal+=salePurchase.getSpjhzj();
		}
		List<SalePurchase> YearXSDetail = fnancialPlansMapper.getMouthXSDetail(DateUtil.getThatDate(startTime,0,1), DateUtil.getThatDate(endTime, 1,1));
		int time=0;
		for (SalePurchase salePurchase : YearXSDetail) {
			XSTotal+=salePurchase.getSpjhzj();
			if (salePurchase.getSpjhsfsj()!=null&&salePurchase.getPlanarrivedtime()!=null) {					
				long a=salePurchase.getSpjhsfsj().getTime()-sdf.parse(salePurchase.getPlanarrivedtime()).getTime();
				time=(int) (a/1000/60/60/24);
				salePurchase.setDays(time);

			}

		}
		List<SZResult> YearZCDetail = fnancialPlansMapper.getMouthZCDetail(DateUtil.getThatDate(startTime,0,1), DateUtil.getThatDate(endTime, 1,1));
		for (SZResult szResult : YearZCDetail) {
			ZCTotal+=szResult.getJHJE();
		}
		List<SZResult> YearSRDetail = fnancialPlansMapper.getMouthSRDetail(DateUtil.getThatDate(startTime,0,1), DateUtil.getThatDate(endTime, 1,1));
		for (SZResult szResult : YearSRDetail) {
			SRTotal+=szResult.getJHJE();
		}
		map.put("CGYear", YearCGDetail);
		map.put("CGYearTotal", CGTotal);
		map.put("XSYear", YearXSDetail);
		map.put("XSYearTotal", XSTotal);
		map.put("ZCYear", YearZCDetail);
		map.put("ZCYearTotal", ZCTotal);
		map.put("SRYear", YearSRDetail);
		map.put("SRYearTotal", SRTotal); 
		return map;
	}


	@Override
	public void updateXSTable(SalePurchase sale) {
		SalePurchase sa=new SalePurchase();
		if (sale.getSpid()!=null&&!"".equals(sale.getSpid())) {
			List<SalePurchase> xssPid = fnancialPlansMapper.getXSSPid(sale.getSpid());
			for (SalePurchase salePurchase : xssPid) {
				if (sale.getSpmc()!=null) {
					sa.setSpmc(sale.getSpmc());
				}else {
					sa.setSpmc(salePurchase.getSpmc());
				}
				if (sale.getSpxh()!=null) {
					sa.setSpxh(sale.getSpxh());
				}else {
					sa.setSpxh(salePurchase.getSpxh());
				}
				if (sale.getSpjhsl()!=null) {
					sa.setSpjhsl(sale.getSpjhsl());
				}else {
					sa.setSpjhsl(salePurchase.getSpjhsl());
				}
				if (sale.getSpjhdj()!=null) {
					sa.setSpjhdj(sale.getSpjhdj());
				}else {
					sa.setSpjhdj(salePurchase.getSpjhdj());
				}
				if (sale.getSpjhdj()!=null&&sale.getSpjhsl()!=null) {
					sa.setSpjhzj(sale.getSpjhdj()*sale.getSpjhsl());
				}else {
					sa.setSpjhzj(salePurchase.getSpjhzj());
				}
				if (sale.getSpjhsfsj()!=null) {
					sa.setSpjhsfsj(sale.getSpjhsfsj());
				}else {
					sa.setSpjhsfsj(salePurchase.getSpjhsfsj());
				}
				if (sale.getPlanarrivedtime()!=null) {
					sa.setPlanarrivedtime(sale.getPlanarrivedtime());
				}else {
					sa.setPlanarrivedtime(salePurchase.getPlanarrivedtime());
				}
				sa.setSpid(sale.getSpid());
			}
			fnancialPlansMapper.updateXSTable(sa);
			System.out.println("更新采购销售信息成功");
		}


	}


	@Override
	public void updateSZTable(SZResult szResult) {
		SZResult sz=new SZResult();
		if (szResult.getIECODE()!=null&&!"".equals(szResult.getIECODE())) {
			List<SZResult> sziecode = fnancialPlansMapper.getSZIECODE(szResult.getIECODE());
			for (SZResult szResult2 : sziecode) {
				if (szResult.getJHJE()!=null) {
					sz.setJHJE(szResult.getJHJE());
				}else {
					sz.setJHJE(szResult2.getJHJE());
				}
				if (szResult.getJHYF()!=null) {
					sz.setJHYF(szResult.getJHYF());
				}else {
					sz.setJHYF(szResult2.getJHYF());
				}

				sz.setIECODE(szResult.getIECODE());
			}
			fnancialPlansMapper.updateSZTable(sz);
			System.out.println("修改收支计划成功");

		}

	}


	@Override
	public List<SzName> getSzName(String SZLX) {

		return fnancialPlansMapper.getSzName(SZLX);
	}

	@Override
	public void deleteSP(String spid) {
		fnancialPlansMapper.deleteSP(spid);

	}


	@Override
	public Map<String, Object> getSaleData(String rwlx, String code) {
		List<SalePurchase> saleData = salePurchaseMapper.getSaleData(rwlx, code);
		List<SalePurchase> bykhxx = salePurchaseMapper.getBykhxx(rwlx);

		Map<String, Object> map=new HashMap<String, Object>();
		for (SalePurchase salePurchase : bykhxx) {
			List<SalePurchase> list=new ArrayList<SalePurchase>();
			String name=salePurchase.getName().replace("单采血浆", "");
			name=name.replace("有限公司", "");
			for (SalePurchase sale : saleData) {
				SalePurchase sa=new SalePurchase();
				if (sale.getKHXX().equals(salePurchase.getKHXX())) {
					sa.setKHXX(sale.getKHXX());
					sa.setName(name);
					sa.setPlanarrivedtime(sale.getPlanarrivedtime());
					sa.setRWLX(sale.getRWLX());
					sa.setSFHRQ(sale.getSFHRQ());
					sa.setSFKRQ(sale.getSFKRQ());
					sa.setSpjhdj(sale.getSpjhdj());
					sa.setSpjhsfsj(sale.getSpjhsfsj());
					sa.setSpjhsl(sale.getSpjhsl());
					sa.setSpjhzj(sale.getSpjhzj());
					sa.setSpmc(sale.getSpmc());
					sa.setSpsjdj(sale.getSpsjdj());
					sa.setSpsjsl(sale.getSpsjsl());
					sa.setSpsjzj(sale.getSpsjzj());
					sa.setSpxh(sale.getSpxh());

				}
				if (sa.getSpmc()!=null) {
					list.add(sa);
				}

			}

			map.put(name,list);
		}		
		return map;
	}

	@Override
	public Map<String, Object> getPayable(String type) {
		List<AccountsPayable> payable = fnancialPlansMapper.getPayable(type);
		int Invoicesum=0;
		int Paysum=0;
		for (AccountsPayable accountsPayable : payable) {
			if (accountsPayable.getInvoicesum()!=null&&!"".equals(accountsPayable.getInvoicesum())) {
				Invoicesum+=accountsPayable.getInvoicesum();
			}
			if (accountsPayable.getPaysum()!=null && !"".equals(accountsPayable.getPaysum())) {
				Paysum+=accountsPayable.getPaysum();
			}
		}
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("Invoicesum", Invoicesum);
		map.put("Paysum",Paysum);
		map.put("detail", payable);
		return map;
	}


	@Override
	public Map<String,Map<String, Object> > getStackingChart(String rwlx) {
		List<String> onlyWeek = DateUtil.getOnlyWeek();
		List<SalePurchase> stackingChart = salePurchaseMapper.getStackingChart(rwlx);
		List<String> customer = salePurchaseMapper.getCustomer(rwlx);
		Map<String, Object> tMap=null;
		Map<String,Map<String, Object> > map=new LinkedHashMap<String, Map<String,Object>>();

		for (String only : onlyWeek) {
			tMap=new HashMap<String, Object>();
			for (String string : customer) {	
				List<SalePurchase> list=new ArrayList<SalePurchase>();				
				for (SalePurchase salePurchase : stackingChart) {					
					if (only.equals(salePurchase.getWhereWeek())&&string.equals(salePurchase.getName())) {
						list.add(salePurchase);	

						tMap.put(string,list);
					} 

				}

			}
			for (String str : customer) {
				if (tMap.get(str)==null) {
					tMap.put(str,null);
				}
			}
			String time=DateUtil.getWeekDays(Integer.valueOf(only.substring(0,4)),Integer.valueOf(only.substring(5)),0);
			map.put(only+time,tMap);
		}
		return map;
	}


	@Override
	public JSONObject getIncomeStacking(String rwlx) {

		List<IncomeStacking> onlyCategory = fnancialPlansMapper.getOnlyCategory(rwlx);//直接返回到类别中
		List<IncomeStacking> byTypeStacking = fnancialPlansMapper.getByTypeStacking(rwlx,"其他收入");
		List<IncomeStacking> fzStacking = fnancialPlansMapper.getByTypeStacking(rwlx, "房租收入");
		List<IncomeStacking> qjStacking3 = fnancialPlansMapper.getByTypeStacking(rwlx, "清洁及停车费");
		List<IncomeStacking> byTypeStacking2 = fnancialPlansMapper.getByTypeStacking(rwlx, "水电收入");
		List<String> onlyWeek = DateUtil.getOnlyWeek();//x轴数据
		List<IncomeStacking> listLen = DateUtil.getListLen(fzStacking);
		List<IncomeStacking> listLen2 = DateUtil.getListLen(qjStacking3);
		List<IncomeStacking> listLen3 = DateUtil.getListLen(byTypeStacking);
		List<IncomeStacking> listLen4 = DateUtil.getListLen(byTypeStacking2);
		JSONObject option=new JSONObject();
		JSONObject tooltip=new JSONObject();
		tooltip.put("trigger","axis");
		JSONObject axisPointer=new JSONObject();
		axisPointer.put("type", "shadow");
		tooltip.put("axisPointer", axisPointer);
		option.put("tooltip", tooltip);

		JSONObject legend=new JSONObject();
		JSONArray data=new JSONArray();	

		for (IncomeStacking Income : onlyCategory) {
			data.add(Income.getOBJECT());
		}
		legend.put("data",data);

		JSONObject grid=new JSONObject();
		grid.put("left", "10%");
		grid.put("right", "10%");
		grid.put("bottom", "10%");
		grid.put("containLabel", true);
		JSONArray datazoom=new JSONArray();
		JSONObject zoom=new JSONObject();
		zoom.put("type","slider");
		datazoom.add(zoom);

		JSONArray xAxis=new JSONArray();
		JSONArray xdata=new JSONArray();			
		JSONObject object=new JSONObject();
		for (String str : onlyWeek) {
			xdata.add("<br>"+str+"</br>");
		}

		object.put("data",xdata);
		object.put("type","category");		
		xAxis.add(object);

		JSONArray yAxis=new JSONArray();
		//JSONArray ydata=new JSONArray();
		JSONObject y=new JSONObject();
		//ydata.add("");
		y.put("type","value");
		yAxis.add(y);

		JSONArray series=new JSONArray();

		JSONArray sdata=new JSONArray();
		JSONObject seriess=new JSONObject();
		for (IncomeStacking qt : listLen) {


			sdata.add(qt.getJHJE());

			seriess.put("name","房租收入");
			seriess.put("type", "bar");
			seriess.put("barWidth","10");
			seriess.put("stack", "guan");
			seriess.put("data", sdata);		
		}
		series.add(seriess);
		System.out.println(series);

		sdata=new JSONArray();
		seriess=new JSONObject();
		for (IncomeStacking qj : listLen2) {

			sdata.add(qj.getJHJE());

			seriess.put("name","清洁及停车费");
			seriess.put("type", "bar");
			seriess.put("barWidth","10");
			seriess.put("stack", "guan");
			seriess.put("data", sdata);		
		}
		series.add(seriess);

		sdata=new JSONArray();
		seriess=new JSONObject();
		for (IncomeStacking qt : listLen3) {

			sdata.add(qt.getJHJE());

			seriess.put("name","其他收入");
			seriess.put("type", "bar");
			seriess.put("barWidth","10");
			seriess.put("stack", "guan");
			seriess.put("data", sdata);		
		}
		series.add(seriess);

		sdata=new JSONArray();
		seriess=new JSONObject();
		for (IncomeStacking qt : listLen4) {

			sdata.add(qt.getJHJE());

			seriess.put("name","水电收入");
			seriess.put("type", "bar");
			seriess.put("barWidth","10");
			seriess.put("stack", "guan");
			seriess.put("data", sdata);		
		}
		series.add(seriess);
		option.put("legend", legend);
		option.put("grid", grid);
		option.put("xAxis", xAxis);
		option.put("yAxis", yAxis);
		option.put("series",series);
		option.put("dataZoom", datazoom);

		return option;
	}

	/**
	 * 修改实际支出周的方法
	 * @throws ParseException
	 */
	private void updateWeek1() throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		List<WeekConvert> getsjWeek = fnancialPlansMapper.getsjWeek();
		for (WeekConvert weekConvert : getsjWeek) {
			String week2 = DateUtil.getWeek2(sdf.parse((weekConvert.getSjsj())));
			fnancialPlansMapper.updateSjweek(week2, weekConvert.getIeid());
		}

	}
	/**
	 * 修改收入支出的实际总额
	 */
	private void updateSjjeTotal(){
		List<SjjeTotal> sjJeTotal = fnancialPlansMapper.getSjJeTotal("1");
		for (SjjeTotal sjje : sjJeTotal) {
			fnancialPlansMapper.updateSJtotal(sjje.getSjje(), sjje.getSjwhereweek(), "1");
		}

		sjJeTotal = fnancialPlansMapper.getSjJeTotal("2");
		for (SjjeTotal sjje : sjJeTotal) {
			fnancialPlansMapper.updateSJtotal(sjje.getSjje(), sjje.getSjwhereweek(), "2");
		}

	}
	/**
	 * 修改销售采购的周
	 * @throws ParseException
	 */
	private void updateSjWeek() throws ParseException {
		List<WeekConvert> date = salePurchaseMapper.getDate();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		for (WeekConvert week : date) {
			String week2 = DateUtil.getWeek2(sdf.parse(week.getSfkrq()));
			salePurchaseMapper.updateSjWeek(week2,week.getSpid());
		}
	}
	private void updateSjTotal(){
		List<SjjeTotal> sjWeek = salePurchaseMapper.getSjWeek("1");
		for (SjjeTotal total : sjWeek) {
			salePurchaseMapper.updateSjTotal(total.getSjje(), total.getSjwhereweek(), "1");
		}
		sjWeek = salePurchaseMapper.getSjWeek("2");
		for (SjjeTotal total : sjWeek) {
			salePurchaseMapper.updateSjTotal(total.getSjje(), total.getSjwhereweek(), "2");
		}
	}
}
